package com.wwz.service;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwz.mapper.AdminMapper;
import com.wwz.pojo.Admin;
import com.wwz.pojo.model.LoginModel;
import org.springframework.stereotype.Service;

/**
 * 管理员接口实现
 * @date 2022/02/18 27:11:38
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {


    @Override
    public LoginModel login(LoginModel loginModel) {

        String userName = loginModel.getUserName();
        String password = loginModel.getPassWord();
        Boolean rememberMe = loginModel.getRememberMe();
        if (rememberMe == null){
            rememberMe = false;
        }
        //根据用户名密码查找
        Admin one = getOne(new LambdaQueryWrapper<Admin>()
                .eq(Admin::getUserName, userName)
                .eq(Admin::getPassWord, password)
                ,false);

        if (one == null) {
            return null;
        }
        StpUtil.login(one.getId(),rememberMe);

        LoginModel model = new LoginModel();
        model.setUserName(one.getUserName());
        model.setSatoken(StpUtil.getTokenValue());

        return model;
    }
}
